﻿using System.Reflection;
using System.Threading.Tasks;
using Sharp.Domain.PlatformBoundedContext.PermissionModule.Events;
using Sharp.Domain.PlatformBoundedContext.StoragerModule.Events;
using Sharp.Domain.PlatformBoundedContext.UserModule.Events;
using Sharp.Domain.PlatformBoundedContext.UserModule.Services;
using Sharp.Events;
using Sharp.Infrastructure.Crosscutting;
using Sharp.Infrastructure.Interfaces;
using Sharp.Infrastructure.IoC;
using Sharp.Infrastructure.Model;

namespace Sharp.Application.Platform
{
    public class Bootstraper : IBootstrapPlugin
    {
        public Assembly[] StartupAssembles { get; set; }
        public int Order { get; set; }

        public void Initialize()
        {
        }

        public Task InitializeAsync()
        {
            return Task.Run(() =>
            {

                EventBus.Instance.Subscribe(typeof(LoginedEventHandler));
                EventBus.Instance.Subscribe(typeof(SendEMailHandler));
                EventBus.Instance.Subscribe(typeof(PermissionEventHandler));
                EventBus.Instance.Subscribe(typeof(StorageEventHandler));

                ServiceLocator.RegisterType<IDataPermission, UserService>();

                if (ServiceLocator.Instance.IsRegistered<Sharp.ServiceContracts.IUserService>())
                {
                    var userService = ServiceLocator.Instance.GetService<Sharp.ServiceContracts.IUserService>();
                    LoggingHandler.LoginFunc = userService.SaveLog;
                }


                this.IsAlready = true;
            });
        }

        public bool IsAlready { get; set; }
    }
}
